package cn.kgc.ubagnet.controller;

import cn.kgc.ubagnet.pojo.ProImg;
import cn.kgc.ubagnet.pojo.Product;
import cn.kgc.ubagnet.service.product.ProductService;
import cn.kgc.ubagnet.utils.ResultEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ProductController {
    @Resource
    ProductService productService;
    @GetMapping("/seleProduct")
    public ResultEntity<PageInfo<Product>> seleProduct(@RequestParam(value = "typeid",required = false,defaultValue = "0") Integer typeid){
        PageInfo<Product> pageInfo = productService.selePro(typeid);
        return ResultEntity.successWithoutData(pageInfo);
    }
    @GetMapping("/seleProducttext")
    public ResultEntity<PageInfo<Product>> seleProducttext(@RequestParam(value = "typeid",required = false,defaultValue = "0") Integer typeid){
        PageInfo<Product> pageInfo = productService.seleByView(typeid);
        return ResultEntity.successWithoutData(pageInfo);
    }

    @GetMapping("/pro/query/by/id")
    @ResponseBody
    public Product QueryProById(Integer id, Model model){
//        model.addAttribute("proid",id);
        Product product = productService.getProById(id);
        if(product!=null) return product;
        else return null;
    }
    //session存储接收到的proid
    @GetMapping("/pro/for/id")
    @ResponseBody
    public int forProduct(Integer proid,HttpSession session){
        session.setAttribute("proid",proid);
        return proid;
    }

    //商品详情页面查询
    @GetMapping("/proimg/query/by/id")
    @ResponseBody
    public ProImg QueryProImgById(HttpSession session){
        ProImg proimg = productService.getProImgById(Integer.parseInt(session.getAttribute("proid").toString()));
        return proimg;
    }

}
